package top.lshaci.learning.gateway.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * GatewayConfig
 *
 * @author lshaci
 * @date 2020/9/25 11:50
 * @since 1.0.0
 */
@Slf4j
@Configuration
public class GatewayConfig {

//    @Bean
//    @Order(-1)
//    public WebFilter apiPrefixFilter() {
//        return new ApiPrefixFilter();
//    }

    /**
     * 跨域过滤器
     */
    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration configuration = new CorsConfiguration();
        // * 代表任意
        configuration.addAllowedHeader("*");
        configuration.addAllowedMethod("*");
        configuration.addAllowedOrigin("*");
        // 是否允许携带cookie
        configuration.setAllowCredentials(true);

        source.registerCorsConfiguration("/**", configuration);
        CorsWebFilter corsWebFilter = new CorsWebFilter(source);

        return corsWebFilter;
    }

}
